<?php


class Core_Loader
{

    private static  $file_path;

    private function  __construct()
    {}

    public static function load_file($file_name, $dir = null, $once = null)
    {
        $incl_path = get_include_path();

        if (null === $dir) {
            set_include_path(self::$file_path . PATH_SEPARATOR . $incl_path);
        } else {
            if (!empty($dir) && (is_array($dir) || is_string($dir))) {
                if (is_array($dir)) {
                    $dir = implode(PATH_SEPARATOR, $dir);
                }
                set_include_path($dir . PATH_SEPARATOR . $incl_path);
            }
        }
        
        self::_include_file($file_name, $once);
        
        return true;
    }

    public static function load_class($class, $dir = null)
    {
        $class_info = explode('_', $class);
        $class_file_name = $class_info[count($class_info)-1] . EXT;

        if (null === $dir) {
            $dir = PRJ;
            for ($i=0; $i<count($class_info)-1;$i++){
                $dir = $dir . $class_info[$i] .DS;
            }
            self::load_file($class_file_name, $dir, true);
        } else {
            self::load_file($class_file_name, $dir, true);
        }

        if (!class_exists($class, false) && !interface_exists($class, false)) {
            throw new Exception("Class \"$class\" was not found in the file");
        }

        return true;
    }

    public static function spl_register()
    {
        if (!spl_autoload_register('Core_Loader::autoload')) {
            throw  new Exception('Could not register class autoload function');
        }
        
    }

    public static function spl_unregister()
    {
        if (!spl_autoload_unregister('Core_Loader::autoload')) {
            throw  new Exception('Could not unregister class autoload function');
        }
    }


    public static function autoload($class_name)
    {
        try {
            @self::load_class($class_name);
            return $class_name;
        } catch (Exception $exc) {
            return false;
        }
    }

    protected static function _include_file($file,$once = false)
    {
        if ($once) {
            return include_once $file;
        } else {
            return include $file;
        }
    }

  


}

?>
